
/**
 * @fileoverview  The stories container.
 * @author johannes.nel@gmail.com (Johan)
 * Date: 2012/03/28
 * Time: 9:51 AM
 */




goog.provide('lfpug.ui.component.TaskComponent');

goog.require('lfpug.model.Task');
goog.require('lfpug.ui.Component');
goog.require('lfpug.ui.renderer.TaskRenderer');



/**
 * @constructor
 * @extends {lfpug.ui.Component}
 *
 * @param {lfpug.ui.type.Model} model
 *        The adaptor.
 *
 * @param {lfpug.ui.Renderer=} opt_renderer
 *        Optional renderer.
 */
lfpug.ui.component.TaskComponent = function(model, opt_renderer) {
  goog.base(this, model, opt_renderer);

};
goog.inherits(
    lfpug.ui.component.TaskComponent,
    lfpug.ui.Component
);


/**
 * @override
 */
lfpug.ui.component.TaskComponent.prototype.setupBindingsInternal = function() {
  var model = /** @type {lfpug.model.Task} */(this.getModel());
  this.onChangeHandler_ = goog.bind(this.onChange_, this);
  //model.addEventListener...
  goog.events.listen(
      model,
      lfpug.model.BaseModel.EventType.CHANGE,
      this.onChangeHandler_,
      undefined,
      this
  );
  var renderer =
      /** @type {lfpug.ui.renderer.TaskRenderer}*/(this.getRenderer());
  this.registerBinding_(
      lfpug.model.Task.Property.HOURS_ESTIMATED,
      lfpug.ui.renderer.TaskRenderer.HOURS_ESTIMATED_TEMPLATE,
      goog.getCssName('hoursEstimated')  /* the resolver for the target node*/,
      renderer.getHoursEstimatedObject /* the template obj func */,
      renderer /* the scope of the template object func */,
      [this] /* arguments to be passed to the template object func */,
      [this.hoursEstimatedChanged_]/* an array of triggers I want executed post
                                this binding */

  );
  this.registerBinding_(
      lfpug.model.Task.Property.HOURS_REMAINING,
      lfpug.ui.renderer.TaskRenderer.HOURS_REMAINING_TEMPLATE,
      goog.getCssName('hoursRemaining'),
      renderer.getHoursRemainingObject,
      renderer,
      [this],
      [this.hoursRemainingChanged_]
  );
};


/**
 * @private
 * @param {lfpug.events.ModelChangeEvent} event The change event.
 */
lfpug.ui.component.TaskComponent.prototype.onChange_ = function(event) {
  var chain = event.getChain();
  var evt = chain[0];
  var changedProp = evt.getChangedProp();
  if (goog.isDefAndNotNull(changedProp)) {
    this.executeBindings_(changedProp);
  }
};


/**
 * @override
 */
lfpug.ui.component.TaskComponent.prototype.teardownBindingsInternal =
    function() {
  var model = /** @type {lfpug.model.Task} */(this.getModel());
  goog.events.unlisten(
      model,
      lfpug.model.BaseModel.EventType.CHANGE,
      this.onChangeHandler_,
      undefined,
      this
  );
};


/**
 * @private
 * @param {lfpug.model.BaseModel.ChangedProperty} prop The change information
 * with the change.
 */
lfpug.ui.component.TaskComponent.prototype.hoursRemainingChanged_ =
    function(prop) {
  if (prop.prevValue < prop.curValue) {
    this.getRenderer().setRemainingHoursColor(this, goog.getCssName('green'));
  } else {
    this.getRenderer().setRemainingHoursColor(this, goog.getCssName('red'));
  }
};


/**
 * @private
 * @param {lfpug.model.BaseModel.ChangedProperty} prop The change information
 * with the change.
 */
lfpug.ui.component.TaskComponent.prototype.hoursEstimatedChanged_ =
    function(prop) {
  if (prop.prevValue < prop.curValue) {
    this.getRenderer().setEstimatedHoursColor(this, goog.getCssName('green'));
  } else {
    this.getRenderer().setEstimatedHoursColor(this, goog.getCssName('red'));
  }
};


/** @override */
lfpug.ui.component.TaskComponent.prototype.setDefaultRenderer = function() {
  var renderer = lfpug.ui.renderer.TaskRenderer.getInstance();
  this.setRenderer(renderer);
};

goog.ui.registry.setDecoratorByClassName(
    lfpug.ui.Renderer.CSS_CLASS,
    function() {
      return new lfpug.ui.Component(null);
    });


lfpug.ui.ComponentFactory.register(
    lfpug.model.Task,
    function(task) {
      return new lfpug.ui.component.TaskComponent(
          /** @type {lfpug.model.Task} */(task));
    }
);

